package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbCommissionExtractinfoService;
import com.ge.modules.api.service.TbCommissionIncomeinfoService;
import com.ge.modules.entity.TbCommissionExtractinfoEntity;
import com.ge.modules.entity.TbCommissionIncomeinfoEntity;
import com.ge.modules.entity.TbStoreEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 收入明细表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-27 11:23:50
 */
@RestController
@RequestMapping("/api/store/commissionincomeinfo")
public class TbStoreCommissionIncomeinfoController {
    @Autowired
    private TbCommissionIncomeinfoService commissionIncomeinfoService;

    @Autowired
    private TbCommissionExtractinfoService commissionExtractinfoService;

    /**
     * 查询我的收入列表
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
        params.put("userId", tbStoreEntity.getStoreId());
        params.put("userType", tbStoreEntity.getUserType());
        //查询列表数据
        Query query = new Query(params);
        List<TbCommissionIncomeinfoEntity> commissionIncomeinfoList = commissionIncomeinfoService.queryList(query);
        int total = commissionIncomeinfoService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(commissionIncomeinfoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 信息
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TbCommissionIncomeinfoEntity commissionIncomeinfo = commissionIncomeinfoService.queryObject(id);
        return R.ok().put("commissionIncomeinfo", commissionIncomeinfo);
    }

    /**
     * 查询钱包余额
     *
     * @param tbStoreEntity
     * @return
     */
    @GetMapping("/wallet")
    public R wallet(@LoginStoreApi TbStoreEntity tbStoreEntity) {
        TbCommissionIncomeinfoEntity tbCommissionIncomeinfoEntity = new TbCommissionIncomeinfoEntity();
        tbCommissionIncomeinfoEntity.setUserId(tbStoreEntity.getStoreId());
        tbCommissionIncomeinfoEntity.setUserType(tbStoreEntity.getUserType());
        //查询店铺所有的金额
        int incomeinfoMoney = commissionIncomeinfoService.walletCount(tbCommissionIncomeinfoEntity);
        //查询店铺所有提现的金额
        TbCommissionExtractinfoEntity tbCommissionExtractinfoEntity = new TbCommissionExtractinfoEntity();
        tbCommissionExtractinfoEntity.setUserId(tbStoreEntity.getStoreId());
        tbCommissionExtractinfoEntity.setUserType(tbStoreEntity.getUserType());
        tbCommissionExtractinfoEntity.setStatus(2);
        int extractinfoMoney = commissionExtractinfoService.queryMoneyTotal(tbCommissionExtractinfoEntity);
        //钱包余额 = 店铺所有收入金额 - 提现成功的金额
        int balance = incomeinfoMoney - extractinfoMoney;
        return R.ok().put("data", balance);
    }

}
